package com.pureut.production.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.production.domain.AmHrGuard;
import com.pureut.production.domain.vo.AmHrGuardVo;
import com.pureut.production.service.AmHrGuardService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/guard")
public class AmHrGuardController extends BaseController {
    @Resource
    private AmHrGuardService amHrGuardService;


    @GetMapping("/list")
    public TableDataInfo list(AmHrGuardVo amHrGuardVo) {
        startPage();
        AmHrGuard amHrGuard = new AmHrGuard();
        amHrGuard.setGuardCode(amHrGuardVo.getGuardCode());
        amHrGuard.setGuardName(amHrGuardVo.getGuardName());
        List<AmHrGuard> list = amHrGuardService.list(new QueryWrapper<>(amHrGuard).orderByDesc("create_time"));
        return getDataTable(list);
    }

    @DeleteMapping("/delete/{guardCode}")
    public AjaxResult remove(@PathVariable("guardCode") Long id) {
        return toAjax(amHrGuardService.removeById(id));
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody AmHrGuardVo amHrGuardVo) {
        AmHrGuard amHrGuard = new AmHrGuard();
        amHrGuard.setGuardCode(amHrGuardVo.getGuardCode());
        amHrGuard.setGuardName(amHrGuardVo.getGuardName());
        amHrGuard.setCreateTime(new Date());
        amHrGuard.setRemark(amHrGuardVo.getRemark());
        return toAjax(amHrGuardService.save(amHrGuard));
    }

    @GetMapping("/view/{guardCode}")
    public AjaxResult view(@PathVariable("guardCode") Long id) {
        return AjaxResult.success(amHrGuardService.getById(id));
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody AmHrGuard amHrGuard) {
        return toAjax(amHrGuardService.updateById(amHrGuard));
    }

    @GetMapping("/getGuard")
    public AjaxResult getGuard() {
        return AjaxResult.success(amHrGuardService.list(new QueryWrapper<AmHrGuard>()
                        .select("distinct guard_code, guard_name"))
                .stream()
                .map(item -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("guardCode", item.getGuardCode());
                    map.put("guardName", item.getGuardName());
                    return map;
                }));
    }

}
